package com.itheima.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.common.entity.PageResult;
import com.itheima.common.entity.QueryPageBean;
import com.itheima.mapper.CheckgroupCheckitemMapper;
import com.itheima.mapper.CheckitemMapper;
import com.itheima.pojo.CheckItem;
import com.itheima.service.CheckitemService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.List;

@Service
public class CheckitemServiceImpl implements CheckitemService {

    @Autowired
    private CheckitemMapper checkitemMapper;

    @Autowired
    private CheckgroupCheckitemMapper checkgroupCheckitemMapper;
    @Autowired
    private CheckitemService checkitemService;

    /**
     * 新增检查项
     * @param checkItem
     */
    public void save(CheckItem checkItem) {
        checkitemMapper.add(checkItem);
    }

    /**
     * 根据id删除检查项
     * @param id
     */
    public void deleteById(Integer id) {
        checkitemMapper.deleteById(id);
    }

    /**
     * 根据检查组id查询检查项id
     * @param checkgroupId
     * @return
     */
    public List<Integer> findCheckItemIdsByCheckGroupId(Integer checkgroupId) {
        List<Integer> ids = checkgroupCheckitemMapper.findCheckItemIdsByCheckGroupId(checkgroupId);
        return ids == null ? Collections.emptyList() : ids;
    }

    /**
     * 编辑检查项
     * @param checkItem
     */
    public void update(CheckItem checkItem) {
        checkitemMapper.update(checkItem);
    }

    /**
     * 分页查询
     * @param queryPageBean
     * @return
     */
    @Override
    public PageResult pageQuery(QueryPageBean queryPageBean) {
        PageHelper.startPage(queryPageBean.getCurrentPage(), queryPageBean.getPageSize());

        Page<CheckItem> page = checkitemMapper.pageQuery(queryPageBean);

        return new PageResult(page.getTotal(),page.getResult());
    }

    /**
     * 查询所有
     * @return
     */
    public List<CheckItem> findAll() {
        List<CheckItem> checkItems = checkitemMapper.findAll();
        return checkItems;
    }

    /**
     * 根据id查询检查项
     * @param id
     * @return
     */
    @Override
    public CheckItem findById(Integer id) {
        return checkitemMapper.findById(id);
    }
}
